#region using

using System;
using System.Text;
using System.Web;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion

/// <summary>
/// Adds an SnapShots links to all external links.
/// </summary>
[Extension("Adds Snap Shots preview to all external links", "1.0", "<a href=\"http://www.thinkedge.com\">Phil Garcia</a>")]
public class SnapShots
{
	static protected ExtensionSettings _settings = null;

	static protected string Key
	{
		get
		{
			return _settings.GetSingleValue("key");
		}
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="SnapShots"/> class.
	/// </summary>
	public SnapShots()
	{
		Post.Serving += new EventHandler<ServingEventArgs>(Page_Header);
		Page.Serving += new EventHandler<ServingEventArgs>(Page_Header);

		ExtensionSettings settings = new ExtensionSettings("SnapShots");
		settings.AddParameter("key", "SnapShots Key", 100, true);
		settings.IsScalar = true;

		ExtensionManager.ImportSettings(settings);
		_settings = ExtensionManager.GetSettings("SnapShots");
	}

	/// <summary>
	/// Handles the Serving event of the Post object.
	/// </summary>
	void Page_Header(object sender, EventArgs e)
	{
		AddSnapShotScript();
	}

	// <script type="text/javascript" src="http://shots.snap.com/ss/9025b7ecb7d6b54df49bca3d19d6f24a/snap_shots.js"></script>
	/// <summary>
	/// Adds the SnapShot script to the page header.
	/// </summary>
	private static void AddSnapShotScript()
	{
		HttpContext context = HttpContext.Current;
		if (context.CurrentHandler is System.Web.UI.Page)
			// Make sure we haven't already added the script to the page header.
			if (context.Items["snapshot"] == null)
			{
				System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
				string js = string.Format("<script type=\"text/javascript\" src=\"http://shots.snap.com/ss/{0}/snap_shots.js\"></script>", Key);
				page.Header.Controls.Add(new System.Web.UI.LiteralControl(js));
				context.Items["snapshot"] = 1;
			}
	}


}